package org.deltaset.meta.internal;

import java.lang.reflect.Method;
import java.util.List;
import org.deltaset.meta.MetaAction;
import org.deltaset.meta.MetaField;

/**
 *
 * @author dmakariev
 */
class MetaActionImpl implements MetaAction {

    private final Class entityClass;
    private final Class executeClass; //could be an intrerface, or the top level class from an hierarchy (extends from abstract)
    private final String methodName;
    private final Method method;
    private final Method disableMethod;
    private final Method hideMethod;
    // private final Method validateMethod;
    private final MetaParameter parameter;
    private final boolean isFactory;
    private final boolean isFinder;
    private final boolean isReturningEntity;
    private final boolean isEnableWarn;
    private final boolean isReturningVoid;
    private final boolean hasParameter;
    private final boolean isContext;
    private final boolean isEntityMember;

    MetaActionImpl(Class entityClass, Class executeClass, Method method, Method disableMethod, Method hideMethod,
            MetaParameter parameter,
            boolean isFactory, boolean isFinder, boolean isReturningEntity, boolean isReturningVoid,
            boolean hasParameter, boolean isContext, boolean isEntityMember) {
        this.entityClass = entityClass;
        this.executeClass = executeClass;
        this.methodName = method.getName();
        this.method = method;
        this.disableMethod = disableMethod;
        this.hideMethod = hideMethod;
        this.parameter = parameter;
        this.isFactory = isFactory;
        this.isFinder = isFinder;
        this.isReturningEntity = isReturningEntity;
        this.isEnableWarn = methodName.startsWith("warn");
        this.isReturningVoid = isReturningVoid;
        this.hasParameter = hasParameter;
        this.isContext = isContext;
        this.isEntityMember = isEntityMember;
    }

    @Override
    public Class getEntityClass() {
        return entityClass;
    }

    @Override
    public String getMethodName() {
        return methodName;
    }

    @Override
    public boolean isEnableWarn() {
        return isEnableWarn;
    }

    private MetaParameter getParameter() {
        return parameter;
    }

    @Override
    public boolean getIsFactory() {
        return isFactory;
    }
    
    @Override
    public boolean isFinder(){
        return isFinder;
    }

    @Override
    public boolean getIsReturningEntity() {
        return isReturningEntity;
    }

    @Override
    public boolean getIsReturningVoid() {
        return isReturningVoid;
    }

    @Override
    public boolean getHasParameter() {
        return hasParameter;
    }
///////package private 

    @Override
    public boolean isContext() {
        return isContext;
    }

    @Override
    public boolean isEntityMember() {
        //executeClass.equals(entityClass);
        return isEntityMember;
    }

    @Override
    public Method getMethod() {
        return method;
    }

    @Override
    public Method getDisableMethod() {
        return disableMethod;
    }

    @Override
    public Method getHideMethod() {
        return hideMethod;
    }

    @Override
    public Class getExecuteClass() {
        return executeClass;
    }

    @Override
    public Object getNewParameterInstance() {
        return (true==hasParameter)?parameter.newInstance():null;
    }

    @Override
    public Object[] prepareParameters(Object parameterValue) {
        return parameter.prepareParameter(parameterValue);
    }

    @Override
    public List<MetaField> getParameterMetaFields() {
       return parameter.getMetaFields();
    }

    @Override
    public String toString() {
        return "MetaActionImpl{" + "entityClass=" + entityClass + ", executeClass=" + executeClass + ", methodName=" + methodName + '}';
    }
    
    
}
